/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { Scr } = require("../engine/XDefine");
const { XButton } = require("../engine/control/XButton");
const { XSelect } = require("../engine/control/XSelect");
const { XStat } = require("../engine/XStat");
const { WsClient2 } = require("../engine/ws/WsClient2");
const { XTools } = require("../engine/XTools");
const { XWorker } = require("../engine/XWorker");
const { XShader } = require("../engine/graphics/X2DInterface");
const { RightMenu } = require("../engine/RightMenu");
const { X2DBeta } = require("../engine/graphics/X2DInterface");
const { XFbo } = require("../engine/graphics/XFbo");
const { Page1Connect } = require("./Page1Connect");
const { XWindow } = require("../engine/control/XWindow");
const { Page0Background } = require("./Page0Background");
const { Page0Loading } = require("./Page0Loading");
const { RosProto } = require("./ros_msg_utils/RosProto");

class XCenter {
    constructor() {
        // const { MyTest } = require("./test");
        // MyTest.open();
        Page0Background.open();
        Page0Loading.open();
    }
    onDraw() {
        XTools.Update();
        WsClient2.processDataQueue();

        let drawList = [];
        XStat.RecursiveStat((obj) => {
            if (obj.onDraw) {
                drawList.push(obj);
            }
        });
        for (let d of drawList) {
            if (d.onDraw()) {
                break;
            }
        }

        // let posx = 5;
        // let posy = 5;
        // let posz = 12;
        // X2DBeta.drawText("FPS:" + XTools.FPS_NUM, posz, posx, posy, 1, 1, 0, -1, -1, 0xff00ff00);
        // posy += posz;
        // X2DBeta.drawText(`logic:${Scr.logicw}x${Scr.logich}:${XTools.GLOBAL_DPR}`, posz, posx, posy, 1, 1, 0, -1, -1, 0xff00ff00);
        // posy += posz;
        // X2DBeta.drawText(`real:${Scr.realw}x${Scr.realh}`, posz, posx, posy, 1, 1, 0, -1, -1, 0xff00ff00);
        // posy += posz;
        // X2DBeta.drawText(WsClient2.isConnected() ? "connected" : "disconnected", posz, posx, posy, 1, 1, 0, -1, -1, 0xff00ff00);
        // posy += posz;
        // X2DBeta.drawText(XTools.MOUSE_POS.x + "," + XTools.MOUSE_POS.y, posz, posx, posy, 1, 1, 0, -1, -1, 0xff00ff00);
        // posy += posz;
        // X2DBeta.drawText(XTools.getOS(), posz, posx, posy, 1, 1, 0, -1, -1, 0xff00ff00);
        // posy += posz;
        // let kk = "";
        // if (XTools.KEY_CTRL) kk += " CTRL";
        // if (XTools.KEY_SHIFT) kk += " SHIFT";
        // if (XTools.KEY_ALT) kk += " ALT";
        // if (kk.length > 0) {
        //     X2DBeta.drawText(kk, posz, posx, posy, 1, 1, 0, -1, -1, 0xff00ff00);
        //     posy += posz;
        // }

        RightMenu.onDraw();
    }
    ////////////////////////////////////////////////////////
    onTouch(msg, x, y) {
        if (RightMenu.Touch(msg, x, y)) {
            return true;
        }
        let touchList = [];
        XStat.RecursiveStat((obj) => {
            if (obj.onTouch) {
                touchList.push(obj);
            }
        }, true);

        for (let obj of touchList) {
            XStat.OFFX = obj.x_;
            XStat.OFFY = obj.y_;
            if (obj.onTouch(msg, x, y)) {
                return true;
            }
        }
    }
    onKey(type, k) {
        if (RightMenu.Key(k)) {
            return true;
        }
        let keyList = [];
        XStat.RecursiveStat((obj) => {
            if (obj.onKey) {
                keyList.push(obj);
            }
        }, true);

        for (let obj of keyList) {
            if (obj.onKey(type, k)) {
                return true;
            }
        }
    }
}

XCenter.pInstance_ = null;
XCenter.gi = function () {
    if (XCenter.pInstance_ == null) XCenter.pInstance_ = new XCenter();
    return XCenter.pInstance_;
}

module.exports = {
    XCenter
}